import { assign } from 'min-dash'
import { isAny } from 'bpmn-js/lib/features/modeling/util/ModelingUtil'
import Constant from '../../Constant'
import Utils from '../../Utils'

/**
 * 完全自定义bpmnjs的ContextPad类
 *
 * @param {*} contextPad
 * @param {*} config
 * @param {*} injector
 * @param {*} translate
 * @param {*} bpmnFactory
 * @param {*} elementFactory
 * @param {*} create
 * @param {*} modeling
 * @param {*} connect
 * @param {*} rules
 */
export default function PhuketContextPadProvider(
  contextPad,
  config,
  injector,
  translate,
  bpmnFactory,
  elementFactory,
  create,
  modeling,
  connect,
  rules
) {
  this.create = create
  this.elementFactory = elementFactory
  this.translate = translate
  this.bpmnFactory = bpmnFactory
  this.modeling = modeling
  this.connect = connect
  this.rules = rules

  config = config || {}
  if (config.autoPlace !== false) {
    this.autoPlace = injector.get('autoPlace', false)
  }

  // 定义这是一个contextPad
  contextPad.registerProvider(this)
}

// 实例化ContextPadProvider并注入参数
PhuketContextPadProvider.$inject = [
  'contextPad',
  'config',
  'injector',
  'translate',
  'bpmnFactory',
  'elementFactory',
  'create',
  'modeling',
  'connect',
  'rules'
]

/**
 * 获取ContextPad列表
 */
PhuketContextPadProvider.prototype.getContextPadEntries = function(element) {
  const model = element.businessObject.$attrs['_model']
  if (!Utils.isEditModel(model)) {
    return
  }

  var { connect, elementFactory, create, autoPlace, modeling } = this

  var businessObject = element.businessObject

  var actions = {}

  /**
   * 连接操作
   *
   * @param {*} event
   * @param {*} element
   */
  function connectAction(event, element) {
    connect.start(event, element)
  }

  /**
   * 删除操作
   *
   * @param {*} event
   */
  function deleteAction(event) {
    event
    modeling.removeElements([element])
  }

  /**
   * 添加操作
   *
   * @param {String} type
   * @param {String} className
   * @param {String} [title]
   * @param {Object} [options]
   *
   * @return {Object} descriptor
   */
  function appendAction(type, className, title, options) {
    if (typeof title !== 'string') {
      options = title
      title = 'Append ' + type.replace(/^bpmn:/, '')
    }

    // 追加开始
    function appendStart(event, element) {
      // 实例化shape
      var attrs = assign({ type: type }, options)
      var shape = elementFactory.createShape(attrs)
      create.start(event, shape, {
        source: element
      })
    }

    // 追加
    var append = autoPlace
      ? function(event, element) {
        // 实例化shape
        var attrs = assign({ type: type }, options)
        var shape = elementFactory.createShape(attrs)
        autoPlace.append(element, shape)
      }
      : appendStart

    return {
      group: 'model',
      className: className,
      title: title,
      action: {
        dragstart: appendStart,
        click: append
      }
    }
  }

  // 如果点击的节点属于bpmn:FlowNode或“bpmn:InteractionNode”类型，则进行如下操作
  if (isAny(businessObject, ['bpmn:FlowNode', 'bpmn:InteractionNode'])) {
    assign(actions, {
      // “注释”功能
      'append.text-annotation': appendAction(
        'bpmn:TextAnnotation',
        'bpmn-icon-text-annotation',
        '添加文本注释'
      )
    })
    // 不为目标类型节点
    if (businessObject.$attrs.type !== Constant.TYPE_TARGET) {
      assign(actions, {
        // “关联”功能
        connect: {
          group: 'connect',
          // className: "bpmn-icon-connection-multi", // 实线和虚线图标
          className: 'bpmn-icon-connection', // 实线图标
          title: '关联其他节点',
          action: {
            click: connectAction,
            dragstart: connectAction
          }
        }
      })
    }
  }

  assign(actions, {
    // “删除”功能
    delete: {
      group: 'edit',
      className: 'bpmn-icon-trash',
      title: '删除',
      action: {
        click: deleteAction
      }
    }
  })

  return actions
}
